package com.quanyan.club.task.tracker;

import com.alibaba.fastjson.JSONObject;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
import com.quanyan.activity.entity.ActivityInfo;
import com.quanyan.activity.service.ActivityOpenService;
import com.quanyan.club.mq.ClubMedalMsg;
import com.quanyan.club.service.ClubSearchBizService;
import com.quanyan.club.service.CppService;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.constants.MqConstants;
import com.quanyan.common.enums.SysClubMedalConditionEnum;
import com.quanyan.common.utils.ObjectUtil;
import com.quanyan.mq.client.QcMsgClient;
import com.quanyan.mq.client.reqeust.MqRequestMsg;
import com.quanyan.mq.client.response.MqResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yue.yuan on 2016/8/4.
 */
@Component
public class ActivityEndNotifyJob implements JobRunner{

    private static Logger logger = Logger.getLogger(ActivityEndNotifyJob.class);

    @Autowired
    private CppService cppService;

    @Autowired
    private ActivityOpenService activityOpenService;

    @Autowired
    private ClubSearchBizService clubSearchBizService;

    @Autowired
    @Qualifier("clubMedalProducerImpl")
    private QcMsgClient clubMedalProducerImpl;

    @Override
    public Result run(JobContext jobContext) throws Throwable {
        Job job = jobContext.getJob();
        if (job == null) {
            return new Result(Action.EXECUTE_EXCEPTION, "任务不存在");
        }
        Map<String, String> params = job.getExtParams();
        logger.warn("俱乐部活动结束时通知任务执行，参数：" + params);
        int activityId = Integer.parseInt(params.get("activityId"));
        String logoUrl = params.get("logoUrl");
        int feeType = Integer.parseInt(params.get("feeType"));
        String posterUrl = params.get("posterUrl");
        String name = params.get("name");
        //2. 判断此俱乐部活动是否已经被取消，暂停
        ActivityInfo activityInfo = activityOpenService.selectOneActivityById(activityId);
        if(null == activityInfo)
        {
            logger.warn("活动结束定时任务调用失败，没有这个活动，活动ID：" + activityId);
            return new Result(Action.EXECUTE_SUCCESS);
        }
        if (activityInfo.getStatus() == 2 || activityInfo.getIsDelete() == 1) {
            logger.warn("活动结束定时任务调用失败，这个活动已经失效，活动ID：" + activityId);
            //活动被取消，或者暂停
            return new Result(Action.EXECUTE_SUCCESS);
        }
        logger.warn("开始发送MQ勋章消息");
        Integer clubId = Integer.parseInt(params.get("clubId"));
        Integer categoryId = Integer.parseInt(params.get("categoryId"));
        Integer medalId = null;
        switch (categoryId)
        {
            case 1://篮球
                medalId = SysClubMedalConditionEnum.valueOf("BASKETBALL_ACTIVIST").getId();
                break;
            case 2://羽毛球
                medalId = SysClubMedalConditionEnum.valueOf("BADMINTON_ACTIVIST").getId();
                break;
            case 3://足球
                medalId = SysClubMedalConditionEnum.valueOf("FOOTBALL_ACTIVIST").getId();
                break;
            case 4://网球
                medalId = SysClubMedalConditionEnum.valueOf("TENNIS_ACTIVIST").getId();
                break;
            case 10://户外
                medalId = SysClubMedalConditionEnum.valueOf("OUTDOORS_ACTIVIST").getId();
                break;
            case 11://骑行
                medalId = SysClubMedalConditionEnum.valueOf("CYCLING_ACTIVIST").getId();
                break;
            case 12://跑步
                medalId = SysClubMedalConditionEnum.valueOf("RUN_ACTIVIST").getId();
                break;

        }
        ClubMedalMsg clubMedalMsg = new ClubMedalMsg();
        clubMedalMsg.setCategoryId(categoryId);
        clubMedalMsg.setMedalId(medalId);
        clubMedalMsg.setClubId(clubId);
        MqRequestMsg mqRequestMsg = new MqRequestMsg();
        mqRequestMsg.setMsgId(String.valueOf(System.currentTimeMillis()));
        mqRequestMsg.setTopic(MqConstants.ClubMedalConsumer.CLUB_MEDAL_TOPIC);
        mqRequestMsg.setTag(MqConstants.ClubMedalConsumer.CLUB_MEDAL_TAGS);
        mqRequestMsg.setBody(ObjectUtil.toByteArray(clubMedalMsg));

        //发送俱乐部勋章消息
        logger.warn("开始发送俱乐部举办活动后的勋章消息：" + JSONObject.toJSONString(mqRequestMsg));
        MqResponse response = clubMedalProducerImpl.syncSend(mqRequestMsg);
        logger.warn("勋章任务返回:" + response.getMqSendStatus());


        clubSearchBizService.updateSignupActivityCountOfClubIndex(clubId, -1);
        activityInfo.setStatus(Constants.BYTE_ZERO);
        activityOpenService.updateTbActivity(activityInfo);
        return new Result(Action.EXECUTE_SUCCESS);
    }
}
